package com.zwb.demo.easyweb.modules.config.service.impl;

import com.zwb.demo.easyweb.common.entity.response.CommonCode;
import com.zwb.demo.easyweb.common.entity.response.ResponseResult;
import com.zwb.demo.easyweb.core.exception.ExceptionCast;
import com.zwb.demo.easyweb.core.utils.ConvertUtils;
import com.zwb.demo.easyweb.modules.config.entity.GenTemplateFilePO;
import com.zwb.demo.easyweb.modules.config.entity.GenTemplatePO;
import com.zwb.demo.easyweb.modules.config.repository.GenTemplateFilePoRepository;
import com.zwb.demo.easyweb.modules.config.repository.GenTemplatePoRepository;
import com.zwb.demo.easyweb.modules.config.service.GenTemplateFileService;
import com.zwb.demo.easyweb.modules.config.service.GenTemplateService;
import com.zwb.demo.easyweb.modules.config.vo.GenTemplateFileVO;
import com.zwb.demo.easyweb.modules.config.vo.GenTemplateVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class GenTemplateFileServiceImpl implements GenTemplateFileService {

    private final GenTemplateFilePoRepository genTemplateFilePoRepository;

    @Autowired
    public GenTemplateFileServiceImpl(GenTemplateFilePoRepository genTemplateFilePoRepository) {
        this.genTemplateFilePoRepository = genTemplateFilePoRepository;
    }


    @Override
    public ResponseResult saveOne(GenTemplateFileVO vo) {
        if (StringUtils.isNotBlank(vo.getId())){
            Optional<GenTemplateFilePO> optional = genTemplateFilePoRepository.findById(vo.getId());
            if (!optional.isPresent()){
                ExceptionCast.cast(CommonCode.ID_NO_EXITS);
            }
            GenTemplateFilePO po = optional.get();
            BeanUtils.copyProperties(vo, po);
            genTemplateFilePoRepository.save(po);
        }else {
            GenTemplateFilePO po = ConvertUtils.convertObject(vo, GenTemplateFilePO.class);
            genTemplateFilePoRepository.save(po);
        }
        return ResponseResult.ok();
    }

    @Override
    public ResponseResult deleteOne(String uuid) {
        if (StringUtils.isBlank(uuid)){
            ExceptionCast.cast(CommonCode.ILLEGAL_PARAM);
        }
        genTemplateFilePoRepository.deleteById(uuid);
        return ResponseResult.ok();
    }

    @Override
    @Transactional
    public void deleteAllByTemplateId(String uuid) {
        genTemplateFilePoRepository.deleteAllByTemplateId(uuid);
    }

    @Override
    public ResponseResult findAllByTemplateId(String uuid) {
        List<GenTemplateFilePO> allPO = genTemplateFilePoRepository.findAllByTemplateId(uuid);
        List<GenTemplateFileVO> allVO = ConvertUtils.convertList(allPO, GenTemplateFileVO.class);
        return ResponseResult.ok().body(allVO);
    }

    @Override
    public ResponseResult findAll() {
        List<GenTemplateFilePO> allPO = genTemplateFilePoRepository.findAll();
        List<GenTemplateFileVO> allVO = ConvertUtils.convertList(allPO, GenTemplateFileVO.class);
        return ResponseResult.ok().body(allVO);
    }
}
